	package browser.service;

	import browser.dao.UserInfo;
	import java.io.*;
	import java.sql.*;
	import javax.servlet.*;
	import javax.servlet.http.*;

	/**
	 * Servlet that will display view source code
	 *
	 */
	public class ProcessViews extends HttpServlet {

		public static Connection conn;
		public static Statement stmt;

		public void doGet(HttpServletRequest request,
				HttpServletResponse response) throws ServletException,
				IOException {

			doPost(request, response);
		}

		public void doPost(HttpServletRequest request,
				HttpServletResponse response) throws ServletException,
				IOException {

			response.setContentType("text/html");
			PrintWriter out = response.getWriter();

			// Check if session is expired
			if (request.getSession(false) == null
					|| (Boolean) request.getSession(false).getAttribute(
							"expired") == true) {
				out.println("Session expired");
				return;
			}

			String[] uid = request.getParameterValues("uid");
			String[] uacc = request.getParameterValues("uAccess");
			String[] vname = request.getParameterValues("vName");

			try {
				Class.forName("oracle.jdbc.driver.OracleDriver");
			} catch (ClassNotFoundException e) {
				out.println("Error loading the Driver:" + e.getMessage());
				return;
			}

			Connection conn = null;
			UserInfo user = MyUtilities.getUserInfo(uid[0]);

			if ((user.getUaccess() == null)
					|| (!uacc[0].equals(user.getUaccess()))) {
				out.print("Please login first");
				return;
			}
			try {
				conn = DriverManager.getConnection("jdbc:oracle:thin:"
						+ user.getUid() + "/" + user.getPassword()
						+ "@oraserver.cs.luc.edu:1521:cs");
			} catch (SQLException e1) {
				out.println("Error connecting to Oracle:" + e1.getMessage());
				return;
			}
			if (conn == null) {
				out.println("Null Connection");
				return;
			}

			Statement stmt = null;
			try {
				stmt = conn.createStatement();
			} catch (SQLException e) {
				out.println("createStatement " + e.getMessage());
				try {
					conn.close();
				} catch (SQLException e2) {
				}
				;
				return;
			}

			out.println("<html>");
			out.println("  <head><title>Views");
			out.println("  </title><link type=\"text/css\" rel=\"stylesheet\" href=\"main.css\"/></head>");
			out.println("  <body>");
			out.println("    <p>");

			ResultSet rset = null;
			String query = "select text from user_views where view_name='"
					+ vname[0] + "'";
			try {
				rset = stmt.executeQuery(query);
			} catch (SQLException e) {
				out.println("executeQuery " + e.getMessage());
				return;
			}

			try {
				while (rset.next()) {
					out.println(rset.getString("text"));
					out.println("</br>");
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}

			out
					.println("<form action=\"/DatabaseBrowser/GetCurrentView\" method=\"post\">");
			out.println("<input type=\"hidden\" name=\"uid\" value=\"" + uid[0]
					+ "\">");
			out.println("<input type=\"hidden\" name=\"uAccess\" value=\""
					+ uacc[0] + "\">");
			out.println("<input type=\"hidden\" name=\"vName\" value=\""
					+ vname[0] + "\">");
			out.println("<input type=\"submit\" value=\"Get Current View\">");
			out.println("</form>");

			out.println("    </p>");
			out.println("  </body>");
			out.println("</html>");

			try {
				stmt.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			out.close();
		}
	}
